﻿using HBFramework;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class Panel_DialogTip2 : BasePanel
{
    public override int RemainTime => PanelRemainTime.Long;

    public override int Layer => PanelLayer.Tip;

    public override string Path => "ui/panel_dialogtip1";

    private Button confirmBtn;
    private TMP_Text confirmTmp;
    private Button cancelBtn;
    private TMP_Text cancelTmp;
    private TMP_Text titleTmp;
    private TMP_Text contentTmp;
    private Animation panelAnim;
    private Action onConfirmAct;
    private Action onCancelAct;

    public override void OnInit(GameObject ui)
    {
        confirmBtn = ui.transform.Find("root_popup/btn_confirm").GetComponent<Button>();
        confirmTmp = ui.transform.Find("root_popup/btn_confirm/tmp_confirm").GetComponent<TMP_Text>();
        cancelBtn = ui.transform.Find("root_popup/btn_cancel").GetComponent<Button>();
        cancelTmp = ui.transform.Find("root_popup/btn_cancel/tmp_cancel").GetComponent<TMP_Text>();
        titleTmp = ui.transform.Find("root_popup/title/tmp_title").GetComponent<TMP_Text>();
        contentTmp = ui.transform.Find("root_popup/bg_content/tmp_content").GetComponent<TMP_Text>();
        panelAnim = ui.transform.GetComponent<Animation>();

        confirmBtn.onClick.AddListener(OnConfirmBtnClick);
        var confirmBtnAnim = new ButtonAnimComp(confirmBtn);
        cancelBtn.onClick.AddListener(OnCancelBtnClick);
        var cancelBtnAnim = new ButtonAnimComp(cancelBtn);
    }

    public override void OnShow()
    {
        base.OnShow();
        panelAnim.Play();
    }

    public override void OnRelease()
    {

    }

    private void OnConfirmBtnClick()
    {
        onConfirmAct?.Invoke();
        PanelManager.Instance.ClosePanel(this);
    }

    private void OnCancelBtnClick()
    {
        onCancelAct?.Invoke();
        PanelManager.Instance.ClosePanel(this);
    }

    public void ShowTipInPanel(string title, string tip, string confirm, string cancel, Action onConfirm, Action onCancel)
    {
        titleTmp.text = title;
        contentTmp.text = tip;
        confirmTmp.text = confirm;
        onConfirmAct = onConfirm;
        cancelTmp.text = cancel;
        onCancelAct = onCancel;
    }

    /// <summary>
    /// 显示提示
    /// </summary>
    /// <param name="tip"></param>
    /// <param name="title"></param>
    /// <param name="confirm"></param>
    /// <param name="onConfirm"></param>
    public static async void ShowTip(string tip, string title = "提示",
        string confirm = "确定", string cancel = "取消", Action onConfirm = null, Action onCancel = null)
    {
        Panel_Mask.ShowMask(100);
        var panel = await PanelManager.Instance.OpenPanel<Panel_DialogTip2>();
        panel.ShowTipInPanel(title, tip, confirm, cancel, onConfirm, onCancel);
        Panel_Mask.HideMask(100);
    }
}
